<?php

/*
 * Autoloading of classes
 * - Begins with /library, and then checks /application
 */
function __autoload($className) {
    if (file_exists('library/' . $className . '.class.php')) {
        require_once('library/' . $className . '.class.php');
    } else if (file_exists('application/controller/' . $className . '.php')) {
        require_once('application/controller/' . $className . '.php');
    } else if (file_exists('application/model/' . $className . '.php')) {
        require_once('application/model/' . $className . '.php');
    } else if (file_exists('application/class/' . $className . '.php')) {
        require_once('application/class/' . $className . '.php');
    } else {
        // error
    }
}


/*
 * Strips slashes if needed for DB
 */
function stripslashes_deep($value){
    $value = is_array($value) ?
                array_map('stripslashes_deep', $value) :
                stripslashes($value);

    return $value;
}



/*
 * Route the URL; and determine instance
 * - e.g. admin
 */
function routeURL($url) {
    global $routing;

    foreach ($routing as $pattern => $result) {
        if (preg_match($pattern, $url)) {
            return preg_replace( $pattern, $result, $url );
        }
    }

    return ($url);
}



/*
 * A main hook that is called every time index.php is loaded
 *  Seperates the URL, defines and init correct controller
 *  ex: domain.com/film/item/1
 */
function callMain($entity, $action, $query) {
    global $default;
    global $post;
    global $get;

    // Check if entity/action is set, otherwise set to default as specified in routing.php
    if(!$entity)
        $entity = ucwords($default['entity']);
    if(!$action)
        $action = $default['action'];

    // String the name of the controller
    $controllerStr = $entity.'Controller';
    // call and init controller by defined string name
    $controllerObject = new $controllerStr($entity, $action, $query, $post, $get);

    if (method_exists($controllerObject, $action)) {
        call_user_func_array(array($controllerObject,$action),$query);
    } else {
       // error
    }

    //return $result;

}


function initCleanUrl(){
    global $url;
    global $post;
    global $get;

    // route URL for e.g. admin
    $url = routeURL($url);

    // set the namespace of URL
    $urlArray = array();
    $urlArray = explode("/",$url);

    // the entity (first character capitilized)
    $entity = ucwords($urlArray[0]);
    array_shift($urlArray);
    // the action
    $action = $urlArray[0];
    array_shift($urlArray);
    // the rest is the query
    $query = $urlArray;

    if(!$entity)
        $entity = $default['entity'];

    callMain($entity, $action, $query);
}

function initUrl(){
    global $post;
    global $get;

    // the entity (first character capitilized)
    $entity = ucwords($get['e']);
    // the action, plain get
    $action = $get['a'];
    // the query, remade as an array for generic implementation
    $queryStr = $get['q'];
    $query = explode(',',$queryStr);


    callMain($entity, $action, $query);

}


function initMain(){
    global $cleanUrl;

    if($cleanUrl)
        initCleanUrl();
    else
        initUrl();

}



// init database
$db = new Database();

// init and call main hook
initMain();

?>